// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router';
import Layout from '../layout/Layout.vue';
import AdminLayout from '../layout/AdminLayout.vue';

const routes = [
  // 可视化大屏路由 - 用于单独访问
  {
    path: '/command-visualization',
    name: 'CommandVisualization',
    component:() => import('../views/dashboard/CommandVisualization.vue'),
    meta: { title: '指挥调度可视化大屏' }
  },
  {
    path: '/DataGenerate',
    name: 'DataGenerate',
    component:() => import('../views/admin/DataGenerate.vue'),
    meta: { title: '数据生成' }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { title: '系统登录' }
  },
  // --- 管理员端路由 ---
  {
    path: '/admin',
    component: AdminLayout,
    redirect: '/admin/personnel',
    children: [
      {
        path: 'personnel',
        name: 'PersonnelManagement',
        component: () => import('../views/admin/PersonnelManagement.vue'),
        meta: { title: '警员管理', requiresAuth: true }
      },
      {
        path: 'equipment',
        name: 'EquipmentManagement',
        component: () => import('../views/admin/EquipmentManagement.vue'),
        meta: { title: '装备管理', requiresAuth: true }
      },
      {
        path: 'scheduling',
        name: 'SchedulingManagement',
        component: () => import('../views/admin/SchedulingManagement.vue'),
        meta: { title: '排班管理', requiresAuth: true }
      },
      {
        path: 'scene',
        name: 'SceneManagement',
        component: () => import('../views/admin/SceneManagement.vue'),
        meta: { title: '场景管理', requiresAuth: true }
      },
      {
        path: 'analytics',
        name: 'Analytics',
        component: () => import('../views/admin/Analytics.vue'),
        meta: { title: '统计分析', requiresAuth: true }
      }
    ]
  },
  // --- 指挥人员端路由 ---
  {
    path: '/',
    component: Layout,
    redirect: '/map',
    children: [
      {
        path: 'map',
        name: 'CommandMap',
        component: () => import('../views/dashboard/CommandMap.vue'),
        meta: { title: '指挥地图', requiresAuth: true }
      },
      {
        path: 'dispatch',
        name: 'Dispatch',
        component: () => import('../views/dashboard/Dispatch.vue'),
        meta: { title: '警力调度', requiresAuth: true },
        props: true
      },
      {
        path: 'incident',
        name: 'Incident',
        component: () => import('../views/dashboard/Incident.vue'),
        meta: { title: '事件处理', requiresAuth: true }
      },
      {
        path: 'monitor',
        name: 'Monitor',
        component: () => import('../views/dashboard/Monitor.vue'),
        meta: { title: '巡逻监控', requiresAuth: true }
      }
    ]
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');

  // 如果访问可视化大屏，不需要身份验证
  if (to.name === 'CommandVisualization') {
    next();
    return;
  }

  // 其他路由的验证逻辑保持不变
  if (to.meta.requiresAuth && !token) {
    next('/login');
  } else if (to.path === '/login' && token) {
    if (token.startsWith('admin')) {
      next('/admin');
    } else {
      next('/');
    }
  } else {
    next();
  }
});

export default router;
